<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form size="mini"
             :model="queryParams"
             ref="queryForm"
             :inline="true"
    >
      <el-form-item>
        <el-button type="success" icon="el-icon-plus" @click="handleAdd" :disabled="isAuth('sys:dict:add')">新增</el-button>
        <el-button type="danger" icon="el-icon-delete" :disabled="multiple || isAuth('sys:dict:delete')" @click="handleDelete">删除
        </el-button>
      </el-form-item>

      <el-form-item prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="字典名称"
            clearable
            @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据表格 -->
    <el-table
        ref="table"
        :data="pageList"
        v-loading="loading"
        @row-click="handleRowClick"
        @selection-change="handleSelectionChange"
        border
        size="medium"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="字典名称" prop="name" width="120"/>
      <el-table-column label="字典编码" prop="code"/>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              circle
              plain
              :disabled="isAuth('sys:dict:update')"
              @click.stop="handleUpdate(scope.row)"
          />
          <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              circle
              plain
              :disabled="multiple || isAuth('sys:dict:delete')"
              @click.stop="handleDelete(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <Pagination
        v-show="pagination.total > 0"
        :total="pagination.total"
        :page.sync="pagination.currentPage"
        :limit.sync="pagination.pageSize"
        :pageSizes=[2,5,10,20]
        @pagination="handleQuery"
    />

    <!-- 弹窗表单 -->
    <el-dialog
        :title="dialog.title"
        :visible.sync="dialog.visible"
        width="500px"
        @close="closeDialog">
      <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-width="80px">
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入字典名称"/>
        </el-form-item>
        <el-form-item label="字典编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入字典编码"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {add, del, detail, getDictPageList, update} from "@/api/system/dict/dict";

export default {
  name: "Dict",
  data() {
    return {
      loading: true,
      ids: [],
      multiple: true,
      queryParams: {
        name: undefined
      },
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      pageList: [],
      dialog: {
        title: undefined,
        visible: false
      },
      form: {
        status: 1
      },
      rules: {
        name: [
          {required: true, message: '请输入字典名称', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入字典编码', trigger: 'blur'}
        ]
      }
    }
  },

  created() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryParams.currentPage = this.pagination.currentPage
      this.queryParams.pageSize = this.pagination.pageSize
      getDictPageList(this.queryParams).then(response => {
        this.pageList = response.data
        this.pagination.total = response.total
        this.loading = false
      })
    },
    handleReset() {
      this.pagination = {
        currentPage: 1,
        pageSize: 10,
        total: 0
      }
      this.queryParams = {
        name: undefined,
        queryMode: 'page'
      }
      this.handleQuery()
    },
    handleRowClick(row) {
      this.$emit('dictClick', row)
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = selection.length < 1
    },
    handleAdd() {
      this.resetForm()
      this.dialog = {
        title: '新增字典',
        visible: true
      }
    },

    handleUpdate(row) {
      this.resetForm()
      this.dialog = {
        title: '修改字典',
        visible: true
      }
      const id = row.id || this.ids
      detail(id).then(response => {
        this.form = response.data
      })
    },

    handleSubmit: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const id = this.form.id
          if (id != undefined) {
            update(this.form).then((res) => {
              if (res.status) {
                this.$message.success('修改成功');
                this.closeDialog()
                this.handleQuery()
              }else {
                this.$message.error('修改失败');
              }
            })
          } else {
            add(this.form).then((res) => {
              if (res.status) {
                this.$message.success('新增成功');
                this.closeDialog()
                this.handleQuery()
              }else {
                this.$message.error('新增失败');
              }
            })
          }
        }
      })
    },
    handleDelete(row) {
      const ids = [row.id || this.ids].join(',')
      this.$confirm('确认删除已选中的数据项？', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(ids).then((res) => {
          if (res.status) {
            this.$message.success('删除成功');
            this.handleQuery();
            this.$emit('resetItem');
          } else {
            this.$message.error(res.msg)
          }

        })
      }).catch(() => {
      })
    },
    closeDialog() {
      this.resetForm()
      this.dialog = {
        title: undefined,
        visible: false
      }
    },
    resetForm() {
      this.form = {
        status: 1
      }
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
    }
  }
}
</script>

<style scoped>

</style>
